package com.kingdee.pda.ui.out_check;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.ScanOutOrderInfoBean;
import com.kingdee.pda.ui.dialog.OutCheckSnListDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：dlj
 * @data：2021/9/16 15:10
 * @des：
 */
public class OutCheckListAdapter extends BaseQuickAdapter<ScanOutOrderInfoBean.StockOutOrderDetailInfosBean, BaseViewHolder> {

    private Context context;
    private int opinionPosition = -1;
    private boolean autoSn = false;

    public void setAutoSn(boolean autoSn) {
        this.autoSn = autoSn;
    }

    public void setOpinionPosition(int opinionPosition) {
        this.opinionPosition = opinionPosition;
    }

    public OutCheckListAdapter(Context context, List<ScanOutOrderInfoBean.StockOutOrderDetailInfosBean> beans) {
        super(R.layout.item_outcheck_list, beans);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, ScanOutOrderInfoBean.StockOutOrderDetailInfosBean item) {
        viewHolder.setText(R.id.tv_skuCode, "SKU编码: " + item.getSkucode())
                .setText(R.id.tv_spec_name, "规格名称: " + item.getSkuname())
                .setText(R.id.tv_barCode, "商品条码: " + revertList(item.getSkubarcode()))
                .setText(R.id.btn_qty, item.getScannedqty() + "/" + item.getQty());

//        ImageView imageView = viewHolder.getView(R.id.iv_goods_pic);
//        int length = item.getImageUrl().length();
//        String imageUrl = "";
//        if (length > 0) {
//            String serverUrl = PreferencesUtils.getServerUrl(App.INSTANCE);
//            imageUrl = serverUrl.substring(0, serverUrl.lastIndexOf("/")) + "/fileserver/" + item.getImageUrl();
//        }
//        GlideUtils.loadImage(context, imageUrl, imageView);

        LinearLayout ll_snNum = viewHolder.getView(R.id.ll_snNum);
        if (!"0".equals(item.getSnOutType())) {
            ll_snNum.setVisibility(View.VISIBLE);
            viewHolder.setText(R.id.tv_snNum, "已扫描序列号数：" + item.getSnlist().size());
            ll_snNum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    OutCheckSnListDialog.showSnListDialog(context, new ArrayList<>(item.getSnlist()));
                }
            });
        }

        if (opinionPosition != -1 && opinionPosition == viewHolder.getLayoutPosition()) {
            viewHolder.setBackgroundColor(R.id.item, context.getResources().getColor(R.color.color_item_bg));
        } else {
            viewHolder.setBackgroundColor(R.id.item, context.getResources().getColor(R.color.color_ffffff));
        }

        if (item.getScannedqty() == item.getQty()) {
            viewHolder.setBackgroundRes(R.id.btn_qty, R.drawable.shape_switch_green_track);
        } else {
            viewHolder.setBackgroundRes(R.id.btn_qty, R.drawable.shape_switch_grey_track);
        }
    }

    private String revertList(List<String> list) {
        String text = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                text = text + list.get(i);
            } else {
                text = text + list.get(i) + ",";
            }
        }
        return text;
    }
}
